package edu.hcp.longshorturl.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hcp.longshorturl.entity.UrlEntity;
import edu.hcp.longshorturl.mapper.UrlMapper;
import edu.hcp.longshorturl.service.IUrlService;
import edu.hcp.longshorturl.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**                                                                                ____________________
      _                _                                                           < 神兽护体，永无bug! >
    | |__  _   _  ___| |__   ___ _ __   __ _ _ __   ___ _ __   __ _                --------------------
   | '_ \| | | |/ __| '_ \ / _ \ '_ \ / _` | '_ \ / _ \ '_ \ / _` |                       \   ^__^
  | | | | |_| | (__| | | |  __/ | | | (_| | |_) |  __/ | | | (_| |                        \  (oo)\_______
 |_| |_|\__,_|\___|_| |_|\___|_| |_|\__, | .__/ \___|_| |_|\__, |                           (__)\       )\/\
                                   |___/|_|                |___/                                ||----w |
                                                                                                ||     ||
 * @author huchengpeng
 * @date 2021/8/21 19:26
 * @version 
 * @Description 
 */
@Service("urlService")
public class UrlServiceImpl extends ServiceImpl<UrlMapper, UrlEntity> implements IUrlService {

    @Autowired
    private UrlMapper urlMapper;

    @Override
    public UrlEntity queryByLongUrl(String longUrl) {
        LambdaQueryWrapper<UrlEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UrlEntity::getLongUrl,longUrl);
        UrlEntity urlEntity = baseMapper.selectList(queryWrapper).get(0);
        return urlEntity;
    }

    @Override
    public boolean insertLongUrl(UrlEntity urlEntity) {
        Integer rows = urlMapper.insertLongUrl(urlEntity);
        if(rows < 1){
            return false;
        }
        return true;
    }

    @Override
    public String shortTolong(String shortUrl){
        Long id = UrlUtils.shortUrlToId(shortUrl);
        UrlEntity urlEntity = this.getById(id);
        if( urlEntity != null){
            return urlEntity.getLongUrl();
        }
        return "http://127.0.0.1";
    }
}